"""
WebSocket业务逻辑和扫码枪相关API函数
"""
import json
import time
from datetime import datetime
from fastapi import HTTPException
from websocket_manager import manager, websocket_publish


async def test_scan_simulation():
    """扫码模拟测试 - 推送项目名称更新"""
    try:
        # 生成测试项目名称
        test_project_name = f"测试项目_{int(time.time())}"
        
        # 构造WebSocket消息
        message = {
            "type": "field_update",
            "timestamp": datetime.now().isoformat(),
            "field_name": "project_name",
            "field_value": test_project_name
        }
        
        # 推送到WebSocket客户端
        websocket_publish(json.dumps(message))
        
        return {
            "success": True,
            "message": "扫码模拟测试成功",
            "project_name": test_project_name,
            "clients_count": len(manager.active_connections)
        }
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"扫码模拟测试失败: {str(e)}")
